package service

import (
	"context"
	"fmt"
	__ "user_srv/basic/proto"
	"user_srv/handler/model"
)

type Server struct {
	__.UnimplementedUserServer
}

// 添加预约
func (s *Server) CreatAppointment(_ context.Context, in *__.CreatAppointmentReq) (*__.CreatAppointmentResp, error) {
	var appointment model.Appointment
	appointment.UserId = uint(in.UserId)
	appointment.CourseId = uint(in.CourseId)

	err := appointment.Create()
	if err != nil {
		return nil, fmt.Errorf("预约创建失败")
	}
	return &__.CreatAppointmentResp{
		AppointmentId: int64(appointment.Id),
	}, nil
}

// 添加器材
func (s *Server) CreatEquipment(_ context.Context, in *__.CreatEquipmentReq) (*__.CreatEquipmentResp, error) {
	var equipment model.Equipment
	equipment.EquipmentName = in.EquipmentRespName
	equipment.EquipmentWeight = in.EquipmentRespWeight
	equipment.EquipmentType = in.EquipmentRespType
	err := equipment.CreateEquipment()
	if err != nil {
		return nil, fmt.Errorf("创建器材失败")
	}
	return &__.CreatEquipmentResp{
		EquipmentId: int64(equipment.Id),
	}, nil
}

// 成为会员
func (s *Server) UpdateUser(_ context.Context, in *__.UpdateUserReq) (*__.UpdateUserResp, error) {
	var user model.User
	user.Id = uint(in.Id)
	user.Member = uint(in.Member)
	err := user.UpdateUser()
	if err != nil {
		return nil, fmt.Errorf("修改成功")
	}
	return &__.UpdateUserResp{
		Id: int64(user.Id),
	}, nil
}

// 查询记录
func (s *Server) GetEquipment(_ context.Context, in *__.GetEquipmentReq) (*__.GetEquipmentResp, error) {
	var GetEquipment model.Equipment
	GetEquipment.Id = uint(in.Id)
	err := GetEquipment.GetEquipment()
	if err != nil {
		return nil, fmt.Errorf("查询失败")
	}
	return &__.GetEquipmentResp{
		EquipmentRespName:   GetEquipment.EquipmentName,
		EquipmentRespWeight: GetEquipment.EquipmentWeight,
		EquipmentRespType:   GetEquipment.EquipmentType,
	}, nil
}

// 删除预约
func (s *Server) DeleteAppointment(_ context.Context, in *__.DeleteAppointmentReq) (*__.DeleteAppointmentResp, error) {
	var appointment model.Appointment
	appointment.Id = uint(in.Id)
	err := appointment.Delete()
	if err != nil {
		return nil, fmt.Errorf("删除成功")
	}
	return &__.DeleteAppointmentResp{
		Id: int64(appointment.Id),
	}, nil
}

// 创建课程
func (s *Server) CreateCourse(_ context.Context, in *__.CreateCourseReq) (*__.CreateCourseReqsp, error) {
	var course model.Course
	course.UserId = uint(in.UserId)
	course.Describe = in.Describe
	course.CoursePrice = float64(in.CoursePrice)
	course.CourseUrl = in.CourseUrl
	err := course.CreateCourse()
	if err != nil {
		return nil, fmt.Errorf("创建课程失败")
	}
	return &__.CreateCourseReqsp{
		Id: int64(course.Id),
	}, nil
}
